
import 'package:card_topia/common_widget/my_app_bar.dart';
import 'package:card_topia/common_widget/my_scaffold.dart';
import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/utils/dialog_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:webview_flutter/webview_flutter.dart';
///扫码结果页面

/*
* 如果扫码结果中包含fowo.fun自己的域名则加载WebView
* 如果没有就展示扫描出来的值即可
* */
class ScanResultPage extends StatefulWidget {
  const ScanResultPage({super.key, this.data});

  final String? data;

  @override
  State<ScanResultPage> createState() => _ScanResultPageState();
}

class _ScanResultPageState extends State<ScanResultPage> {
  WebViewController? _webviewController;
  String _title = "";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.data?.contains("fowo.fun")??false) {
      _webviewController = WebViewController()
        ..setJavaScriptMode(JavaScriptMode.unrestricted)
        ..setBackgroundColor(const Color(0x00000000))
        ..setNavigationDelegate(
          NavigationDelegate(
            onProgress: (int progress) {
              // Update loading bar.
            },
            onPageStarted: (String url) {},
            onPageFinished: (String url) {},
            onWebResourceError: (WebResourceError error) {},
            // onNavigationRequest: (NavigationRequest request) {
              // if (request.url.startsWith('https://www.youtube.com/')) {
              //   return NavigationDecision.prevent;
              // }
              // return NavigationDecision.navigate;
            // },
          ),
        )
        ..loadRequest(Uri.parse('https://www.baidu.com'))
        ..getTitle().then((value) => setState(() => _title = value ?? ""));
    }else{
      _title = "错误";
      Future.delayed(Duration.zero,(){
        DialogUtil.shwCenterDialog(context,title: "提示",content: "非平台二维码",singleBtn: true);
      });
    }

  }
  @override
  void dispose() {
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  CustomAppBar(
        showLeading: true,
        title: _title,
      ),
      body: (widget.data?.contains("fowo.fun") ?? false)
          ? WebViewWidget(controller: _webviewController!)
          : Container(
          margin: EdgeInsets.only(left: 16.w),
          child: SelectableText(widget.data??"",style: TextStyle(fontSize: 16.sp,color: descColor,fontFamily: harmony),)),
      // floatingActionButton: FloatingActionButton(
      //   child: Text("+"),
      //   onPressed: (){
      //     DialogUtil.shwCenterDialog(context,title: "提示",content: "非平台二维码",singleBtn: true);
      //   },
      // ),
    ).withOnTap(context);
  }
}
